package com.android.fastride.model.repository;

import com.android.fastride.FastRideApplication;
import com.android.fastride.R;
import com.android.fastride.exception.NotLoginException;
import com.android.fastride.manager.UserManager;
import com.android.fastride.model.db.GreenDaoManager;
import com.android.fastride.model.db.entity.FinishedOrdersCountResponse;
import com.android.fastride.model.db.entity.LoginLog;
import com.android.fastride.model.db.entity.OrderResponse;
import com.android.fastride.model.http.entity.LoginUserInfoEntity;
import com.android.fastride.model.db.entity.FastRideMessage;
import com.peng.library.mantis.kit.Kits;

import java.util.Date;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;

/**
 * Created by admin on 2017/9/22.
 */

public class ControlSystemRepository {

	/**
	 * 获取验证码
	 *
	 * @param phoneNum
	 * @param deviceId
	 * @return
	 */
	public static Observable<String> getVerifyCode (String phoneNum, String deviceId) {
		return null;
		/*return FastRideApplication
				.getApp()
				.getControlSysService()
				.getCheckCode(phoneNum, deviceId);*/
	}


	/**
	 * 通过密码登录
	 *
	 * @param phoneNum
	 * @param pwd
	 * @param deviceId
	 * @return
	 */
	public static Observable<LoginUserInfoEntity> loginByPwd (String phoneNum, String pwd, String deviceId) {
		return FastRideApplication
				.getApp()
				.getControlSysService()
				.loginByPwd(phoneNum, pwd)
				.doOnNext(loginUserInfoEntity -> {
					/*LoginLog loginLog = new LoginLog();
					loginLog.setLoginDate(new Date());
					loginLog.setLoginType(FastRideApplication.getApp().getString(R.string.login_by_pwd));*/
					//确定已经登录成功，开始写日志
					/*GreenDaoManager.getInstance().getDaoSession().getLoginLogDao().insert(loginLog);*/
				});
	}

/**
	 * 进行自动登录的相关操作
	 *
	 * @return
	 */

	public static Observable<String> autoLogin () {
		return Observable.<String>unsafeCreate(new Observable.OnSubscribe<String>() {
			@Override
			public void call (Subscriber<? super String> subscriber) {
				//获取用户的token
				String userToken = UserManager.getInstance().getUserToken();
				if (Kits.Empty.check(userToken)) {
					subscriber.onError(new NotLoginException());
				} else {
					subscriber.onNext(userToken);
					subscriber.onCompleted();
				}
			}
		}).flatMap(new Func1<String, Observable<String>>() {
			@Override
			public Observable<String> call (String s) {
				return FastRideApplication.getApp().getControlSysService()
						.autoLogin(s, FastRideApplication.getApp().getDeviceId());
			}
		}).doOnNext(loginUserInfoEntity -> {
			LoginLog loginLog = new LoginLog();
			loginLog.setLoginDate(new Date());
			loginLog.setLoginType(FastRideApplication.getApp().getString(R.string.login_by_auto));
			//确定已经登录成功，开始写日志
			GreenDaoManager.getInstance().getDaoSession().getLoginLogDao().insert(loginLog);
		});

	}


	/**
	 * 获取当前所有分析设备的坐标
	 *
	 * @return
	 *//*
	public static Observable<List<DeviceListBody.NoticeBean>> getDeviceList () {
		return FacingApplication.getApp()
				.getControlSysService()
				.getDeviceList(1, 30)
				.map(new Func1<DeviceListBody, List<DeviceListBody.NoticeBean>>() {
					@Override
					public List<DeviceListBody.NoticeBean> call (DeviceListBody deviceListBody) {
						return deviceListBody.getList();
					}
				});
	}*/

	/**
	 * 获取当前所有摄像头的坐标
	 *
	 * @return
	 *//*
	public static Observable<List<CollectDeviceListBody.NoticeBean>> getCollectDeviceList () {
		return FacingApplication.getApp()
				.getControlSysService()
				.getCameraDeviceList(1, 30)
				.map(new Func1<CollectDeviceListBody, List<CollectDeviceListBody.NoticeBean>>() {
					@Override
					public List<CollectDeviceListBody.NoticeBean> call (CollectDeviceListBody deviceListBody) {
						return deviceListBody.getList();
					}
				});
	}*/

/*	*//**
	 * 获取登录日志
	 *
	 * @return
	 *//*
	public static Observable<List<LoginLog>> getLoginLogList () {
		return GreenDaoManager.getInstance().getDaoSession()
				.getLoginLogDao()
				.queryBuilder()
				.orderDesc(LoginLogDao.Properties.Lid)
				.rx()
				.list();
	}*/

	/**
	 * 退出登录
	 */
	public static Observable<Object> logout () {
		return Observable.unsafeCreate(new Observable.OnSubscribe<Object>() {
			@Override
			public void call (Subscriber<? super Object> subscriber) {
				//清除用户的token
				UserManager.getInstance().clearUserInfo(FastRideApplication.getApp());
				//清除登录日志
				GreenDaoManager.getInstance().getDaoSession().getLoginLogDao().deleteAll();
				subscriber.onNext(null);
				subscriber.onCompleted();
			}
		});
	}
/*

	*/
/**
	 * 获取登录用户信息
	 *
	 * @return
	 *//*
	public static Observable<UserInfo.UserBean> getLoginUserInfo () {
		return FacingApplication.getApp()
				.getControlSysService()
				.getLoginUserInfo()
				.map(new Func1<UserInfo, UserInfo.UserBean>() {
					@Override
					public UserInfo.UserBean call (UserInfo userInfo) {
						return userInfo.getUser();
					}
				});
	}
*/

    /**
	 * 获取已经识别的列表
	 *
	 * @param cameraNo
	 * @param photoNum
	 * @param maxId
	 * @return
	 */
   /*
	public static Observable<List<RecognizedEntity>> getRecognizedList (String cameraNo, int photoNum, long maxId) {
		return FacingApplication.getApp().getCameraService().getRecognizedRespondBody(cameraNo, photoNum, maxId)
				.map(new Func1<RecognizedRespondBody, List<RecognizedEntity>>() {
					@Override
					public List<RecognizedEntity> call (RecognizedRespondBody recognizedRespondBody) {
						return recognizedRespondBody.getData();
					}
				});
	}
*/

/*
	*/
/**
	 * 获取尚未识别的列表
	 *
	 * @param cameraNo
	 * @param photoNum
	 * @param maxId
	 * @return
	 *//*

	public static Observable<List<UnrecognizedEntity>> getUnrecognizedList (String cameraNo, int photoNum, long maxId) {
		return FacingApplication.getApp().getCameraService().getUnrecognizedRespondBody(cameraNo, photoNum, maxId)
				.map(new Func1<UnrecognizedRespondBody, List<UnrecognizedEntity>>() {
					@Override
					public List<UnrecognizedEntity> call (UnrecognizedRespondBody unrecognizedRespondBody) {
						return unrecognizedRespondBody.getData();
					}
				});
	}
*/
/**
	 * 修改用户密码
	 * @param oldPwd
	 * @param newPwd
	 * @return
	 *//*

	public static Observable<ModifyPasswordEntity> modifyPassword(String oldPwd, String newPwd) {
		return FastRideApplication.getApp().getControlSysService().modifyPWD(new ModifyPasswordRequestEntity(oldPwd, newPwd))
				*/
/*.map(new Func1<ModifyPasswordEntity, ModifyPasswordEntity>() {
					@Override
					public ModifyPasswordEntity call(ModifyPasswordEntity modifyPasswordEntity) {
						return modifyPasswordEntity;
					}
				})*//*
;

	}
*/
	/**
	 * 获取通知
	 * @param row
	 * @param page
	 * @return
	 */
	public static Observable<FastRideMessage> getNotice(int row, int page) {
		return FastRideApplication.getApp().getControlSysService().getNotice(row, page);
	}

	/**
	 * 获取新任务列表
	 * @param row
	 * @param page
	 * @return
	 */
	public static Observable<OrderResponse> getPendingOrders(int row, int page) {
		return FastRideApplication.getApp().getControlSysService().getPending(row, page);
	}

	/**
	 *待取货任务列表
	 * @param row
	 * @param page
	 * @return
	 */
	public static Observable<OrderResponse> getPickupOrders(int row, int page) {
		return FastRideApplication.getApp().getControlSysService().getPickup(row, page);
	}

	/**
	 * 配送中
	 * @param row
	 * @param page
	 * @return
	 */
	public static Observable<OrderResponse> getDistributionOrders(int row, int page) {
		return FastRideApplication.getApp().getControlSysService().getDistributionList(row, page);
	}

	/**
	 * 已完成（已送达）
	 * @param row
	 * @param page
	 * @return
	 */
	public static Observable<OrderResponse> getFinishedOrders(int row, int page, String
			startDate, String endDate) {
		return FastRideApplication.getApp().getControlSysService().getFinishedOrderList(row,
				page, startDate, endDate);
	}

	/**
	 * 已取消
	 * @param row
	 * @param page
	 * @return
	 */
	public static Observable<OrderResponse> getCancelOrders(int row, int page, String
			startDate, String endDate) {
		return FastRideApplication.getApp().getControlSysService().getCancelOrders(row, page,
				startDate, endDate);
	}

	/**
	 * 已完成统计
	 * @return
	 */
	public static Observable<FinishedOrdersCountResponse> getFinishedCount() {
		return FastRideApplication.getApp().getControlSysService().getFinishedList();
	}

}
